﻿using GKQC.Common;
using Microsoft.SqlServer.Server;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;

namespace GKQC.Machine
{
    public class LcrMeter11022 : ILcrMeter
    {
        public bool Check(SerialPort sp)
        {
            bool flag = false;
            return flag;
        }
        public void SetFreq(SerialPort sp, string freq, string freq2,string testItem)
        {
            if (sp.IsOpen)
            {
                //string testItem = this.cbTestItem.SelectedValue.ToString();
                byte[] SendBytes = null;
                //string SendData = ":SOURce:FREQuency 120;:SOURce:FREQuency?\r\n";

                string SendData = string.Empty;
                if (string.IsNullOrEmpty(freq)) freq = "120";
                //if(testItem.Contains("CD"))
                    SendData = ":SOURce:FREQuency " + freq + "\r\n";
                //else
                //    SendData = ":SOURce:FREQuency " + freq2 + "\r\n";

                SendBytes = System.Text.Encoding.Default.GetBytes(SendData);
                sp.Write(SendBytes, 0, SendBytes.Length);//发送数据
            }
        }
        public void Fetch(SerialPort sp, string testItem)
        {
            if (sp.IsOpen)
            {
                byte[] SendBytes = null;
                string SendData = ":CALCulate1:FORMat CS;:CALCulate2:FORMat D\r\n";

                switch (testItem)
                {
                    case "CD"://"CAP和DF";
                        SendData = ":CALCulate1:FORMat CS;:CALCulate2:FORMat D\r\n";
                        break;
                    case "R"://"ESR";
                        SendData = ":CALCulate1:FORMat CS;:CALCulate2:FORMat RS\r\n";
                        break;
                    case "Z"://"Z";
                        SendData = ":CALCulate1:FORMat ZS;:CALCulate2:FORMat PHASe\r\n";
                        break;
                    case "ZR"://"Z/ESR";
                        SendData = ":CALCulate1:FORMat ZS;:CALCulate2:FORMat RS\r\n";
                        break;
                }
                SendData += "FETCh?\r\n";
                SendBytes = System.Text.Encoding.Default.GetBytes(SendData);
                sp.Write(SendBytes, 0, SendBytes.Length);//发送数据
            }
        }
        public string GetData(SerialPort sp)
        {
            try
            {
                byte[] readBuffer = new byte[sp.BytesToRead];
                sp.Read(readBuffer, 0, readBuffer.Length);
                return System.Text.Encoding.ASCII.GetString(readBuffer);
            }
            catch (IOException IOEx)
            {
                Logger.LogError(IOEx);
            }
            catch (InvalidOperationException IVOEx)
            {
                Logger.LogError(IVOEx);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
            }
            return "";
        }

        public bool AnalysisData(string str, out decimal cap, out decimal df, out decimal z, out decimal r, string testItem
            ,string zrfreq,bool isRaw)
        {
            cap = df = z = r = 0m; //初始化传出参数
            string strData = string.Empty;
            try
            {
                strData = str.ToLower();
                char[] charSeparators = new char[] { ';' };
                string[] a = strData.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
                if (a.Length < 1)
                {
                    return false;
                }
                strData = a[0];
                //data = data.Replace("+", "").ToLower();
                //CSdata = "+0,+6.36921E-06,+1.34525E+00";
                //ZSdata = "+0,+2.70311E-01,-1.17568E+02";
                charSeparators = new char[] { ',' };
                string[] dataArr = strData.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
                if (dataArr.Length != 3)
                {
                    return false;
                }
                string data1 = dataArr[1];
                string data2 = dataArr[2];
                if (dataArr[0] != "+0" || string.IsNullOrEmpty(data1) || string.IsNullOrEmpty(data2))  //第一个值为0表示数据正确
                {
                    return false;
                }

                if (string.IsNullOrEmpty(str))
                    return false;

                decimal data11 = Common.Tools.Convert2Decimal(data1);
                decimal data22 = Common.Tools.Convert2Decimal(data2);

                switch (testItem)
                {
                    case "CD":
                        cap = Math.Round(data11 * 1000000m, 3);
                        df = (Math.Round(data22 * 100m, 2));
                        break;
                    case "R":
                        r = (Math.Round(data22 * 1000m, 3));
                        break;
                    case "Z":
                        z = (Math.Round(data11 * 1000m, 3)); //data1.ToString();
                        break;
                    case "ZR":
                        z = (Math.Round(data11 * 1000m, 3)); //data1.ToString();
                        r = (Math.Round(data22 * 1000m, 3));
                        break;
                    default:
                        break;
                }

                return true;
            }
            catch (System.Exception ex)
            {
                Logger.LogError(ex);
                return false;
            }
        }

        public void Open(SerialPort sp)
        {
            //todo: 11022待測試開路 :SENS:CORR:COLL:ACQ:STAN 1
            if (sp.IsOpen)
            {
                byte[] SendBytes = null;
                string SendData = ":SENS:CORR:COLL:ACQ:STAN 1" + Environment.NewLine;
                SendBytes = System.Text.Encoding.Default.GetBytes(SendData);
                sp.Write(SendBytes, 0, SendBytes.Length);//发送数据
            }
        }

        public void Short(SerialPort sp)
        {
            //todo:11022短路 待測試 SENS:CORR:COLL:ACQ:STAN 2
            if (sp.IsOpen)
            {
                byte[] SendBytes = null;
                string SendData = ":SENS:CORR:COLL:ACQ:STAN 2" + Environment.NewLine;
                SendBytes = System.Text.Encoding.Default.GetBytes(SendData);
                sp.Write(SendBytes, 0, SendBytes.Length);//发送数据
            }
        }

        public void Remote(SerialPort sp)
        {
           
        }

        public void Local(SerialPort sp)
        {
           
        }
    }
}
